$(document).ready(function(){

    //日期插件初始化
    $('#searchDate').datepicker({
        autoclose: true, //自动关闭
        beforeShowDay: $.noop,    //在显示日期之前调用的函数
        calendarWeeks: false,     //是否显示今年是第几周
        clearBtn: true,          //显示清除按钮
        daysOfWeekDisabled: [],   //星期几不可选
        endDate: new Date((new Date()).getTime() - 24*60*60*1000),        //日历结束日期
        forceParse: true,         //是否强制转换不符合格式的字符串
        format: 'yyyy-mm-dd',     //日期格式
        keyboardNavigation: true, //是否显示箭头导航
        language: "zh-CN",           //语言
        minViewMode: 0,
        orientation: "auto",      //方向
        rtl: false,
        startDate: -Infinity,     //日历开始日期
        startView: 0,             //开始显示
        todayBtn: false,          //今天按钮
        todayHighlight: false,    //今天高亮
        weekStart: 0              //星期几是开始
    }).on('changeDate',setSelectDate());;


    //定时刷新日志
    startQuerySystemInfo();

    //开启查询结果
    startQueryResult();

    //检查系统警报
    startQueryWarning();

    startGetDieCount();

    startgetNewIDData();


    GetRealBetFlag();
    //绑定按钮
    $("#btnStopSystem").click(function () {
        stopTask();
    });

    $("#btnBeginBet").click(function(){
        beginBet();
    });

    $("#btnClearInfo").click(function(){
        clearInfo();
    });

    $("#btnClearDieCount").click(function(){
        clearDieCount();
    });

    $("#btnSetReversal").click(function(){
        SetReversal();
    });

    $("#btnRealBetFlag").click(function(){
        SetRealBetFlag();
    });


    //获取开车状态
    GetCarStatus();
//图表初始化
    var data = null;
    chart = new G2.Chart({
        id: 'visualizationDiv',
        forceFit: true,
        height: 380
    });
    chart.source(data, {
        buydateId: {
            alias: '期数',
            range: [0, 0.5]
        },
        buyMoneytake: {
            alias: '盈亏'
        }
    });
    chart.guide().line([0,0],[0,20000]);
    chart.line().position('buydateId*buyMoneytake').size(2);
    chart.guide().line([0,0],[20000,0],{stroke :'#FF0000'});
    chart.render();
    $('#myTab li:eq(1) a').tab('show');
    $('#myTab li:eq(0) a').tab('show');



});




var g_url = "http://127.0.0.1:8988";

var timeStartQuerySystemInfo;
var timeStartQueryResult;
var timeStartQueryWarning;
var chart;
//定时查询服务器日志
function startQuerySystemInfo(){
    timeStartQuerySystemInfo = window.setInterval(querySystemInfo,1*5000);
}
function startQueryResult(){
    timeStartQueryResult = window.setInterval(queryResult,10*1000);
}
function startQueryWarning(){
    timeStartQueryWarning = window.setInterval(querywarning,5*1000);
    GetRealBetFlag();
}

/**
 * 定时获取穿版数
 */
function startGetDieCount(){
    timeStartQueryWarning = window.setInterval(getDieCount,15*1000);
}

/**
 * 定时获取车辆状态
 */
function startgetNewIDData(){
    timeStartQueryWarning = window.setInterval(getNewIDData,15*1000);
}


function clearInfo(){

    showLoading();
    $.post("/clearInfo",function(data){

        if( data.status == "success" ){
            alert("清除缓存成功！");
        }else{
            alert("清除缓存失败！"+data.message);
        }

    });
}

function queryResult(){
    $.post("/queryResult",function(data){
        if( data.status == "success" ){
            var shtml ="";
            //遍历数组
            $.each(data.list,function(i,e){
                shtml+="<h6 class=\"row\">"+e.message+"</h6>"
            });
            $("#divQueryResult").html(shtml);
        }else{
            $("#divQueryResult").html(data.message);
        }

    });



    //查询盈亏
    $.post("/queryMoneytake",function(data){
        if( data.status == "success" ){
            $("#spanAllmoneytake").html( data.allmoneytake );
            $("#spanAllRealMoneytake").html( data.allRealMoneytake );
            $("#spanAllmoneytakeByDate").html( data.allmoneytakeByDate );
            $("#spanAllRealMoneytakeByDate").html( data.allRealMoneytakeByDate );
        }else{
            $("#divYkMsg").html(data.message);
        }
    });

}
function querySystemInfo(){
    $.post("/querySystemInfoJson",function(data){
        if( data.status == "success" ){
            var shtml ="";
            //遍历数组
            $.each(data.list,function(i,e){
                shtml+="<h6 class=\"row\">"+e.message+"</h6>"
            });
            $("#divSystemInfo").html(shtml);
        }else{
            $("#divSystemInfo").html(data.message);
        }

    });
}
function beginBet(){
    showLoading();
    $.post("/initSystemParaAndBeginBet",$("#formSystemPara").serialize(),function(data){
        if( data.status == "success" ){
            alert("开始下单！");
        }else{
            alert("执行下单失败!原因["+data.message+"]请停止系统重试或重启系统！");
        }

    });

}
function stopTask(){
    showLoading();
    $.post("/stopSystem",function(data){
        if( data.status == "success" ){
            alert("系统任务成功停止！");
        }else{
            alert("系统任务停止失败！");
        }

    });
}

function querywarning(){
    $.post("/querywarning",function(data){
        if( data.status == "success" ){

            $("#divWarning").html("<iframe src='/warning.mp3'></iframe>");
            // $.get("/warning.mp3",function(data){
            //
            // });
        }else{
            return;
        }

    });
}


function showLoading(){
    $("#fakeloader").fakeLoader({
        //timeToHide:1200, //Time in milliseconds for fakeLoader disappear
        //zIndex:999, // Default zIndex
        spinner:"spinner3"//Options: 'spinner1', 'spinner2', 'spinner3', 'spinner4', 'spinner5', 'spinner6', 'spinner7'
        //bgColor:"#2ecc71", //Hex, RGB or RGBA colors
        //imagePath:"yourPath/customizedImage.gif" //If you want can you insert your custom image
    });
}

function hideLoading(){
    $("#fakeloader").fadeOut();

}

/**
 * 获取设置开车状态
 * @constructor
 */
function GetCarStatus() {
    $.post("/getCarStatusAll",function(data){
        if( data.status == "success" ){
            var json = data.data;
            if(json.dx1 == true){
                $("#dx1").attr("checked", true);
                $("#dx1").val(true);
            }else{
                $("#dx1").attr("checked", false);
                $("#dx1").val(false);
            }
            if(json.dx2 == true){
                $("#dx2").attr("checked", true);
                $("#dx2").val(true);
            }else{
                $("#dx2").attr("checked", false);
                $("#dx2").val(false);
            }
            if(json.dx3 == true){
                $("#dx3").attr("checked", true);
                $("#dx3").val(true);
            }else{
                $("#dx3").attr("checked", false);
                $("#dx3").val(false);
            }
            if(json.dx4 == true){
                $("#dx4").attr("checked", true);
                $("#dx4").val(true);
            }else{
                $("#dx4").attr("checked", false);
                $("#dx4").val(false);
            }
            if(json.dx5 == true){
                $("#dx5").attr("checked", true);
                $("#dx5").val(true);
            }else{
                $("#dx5").attr("checked", false);
                $("#dx5").val(false);
            }
            if(json.dx6 == true){
                $("#dx6").attr("checked", true);
                $("#dx6").val(true);
            }else{
                $("#dx6").attr("checked", false);
                $("#dx6").val(false);
            }
            if(json.dx7 == true){
                $("#dx7").attr("checked", true);
                $("#dx7").val(true);
            }else{
                $("#dx7").attr("checked", false);
                $("#dx7").val(false);
            }
            if(json.dx8 == true){
                $("#dx8").attr("checked", true);
                $("#dx8").val(true);
            }else{
                $("#dx8").attr("checked", false);
                $("#dx8").val(false);
            }
            if(json.dx9 == true){
                $("#dx9").attr("checked", true);
                $("#dx9").val(true);
            }else{
                $("#dx9").attr("checked", false);
                $("#dx9").val(false);
            }
            if(json.dx10 == true){
                $("#dx10").attr("checked", true);
                $("#dx10").val(true);
            }else{
                $("#dx10").attr("checked", false);
                $("#dx10").val(false);
            }

            if(json.ds1 == true){
                $("#ds1").attr("checked", true);
                $("#ds1").val(true);
            }else{
                $("#ds1").attr("checked", false);
                $("#ds1").val(false);
            }
            if(json.ds2 == true){
                $("#ds2").attr("checked", true);
                $("#ds2").val(true);
            }else{
                $("#ds2").attr("checked", false);
                $("#ds2").val(false);
            }
            if(json.ds3 == true){
                $("#ds3").attr("checked", true);
                $("#ds3").val(true);
            }else{
                $("#ds3").attr("checked", false);
                $("#ds3").val(false);
            }
            if(json.ds4 == true){
                $("#ds4").attr("checked", true);
                $("#ds4").val(true);
            }else{
                $("#ds4").attr("checked", false);
                $("#ds4").val(false);
            }
            if(json.ds5 == true){
                $("#ds5").attr("checked", true);
                $("#ds5").val(true);
            }else{
                $("#ds5").attr("checked", false);
                $("#ds5").val(false);
            }
            if(json.ds6 == true){
                $("#ds6").attr("checked", true);
                $("#ds6").val(true);
            }else{
                $("#ds6").attr("checked", false);
                $("#ds6").val(false);
            }
            if(json.ds7 == true){
                $("#ds7").attr("checked", true);
                $("#ds7").val(true);
            }else{
                $("#ds1").attr("checked", false);
                $("#ds1").val(false);
            }
            if(json.ds8 == true){
                $("#ds8").attr("checked", true);
                $("#ds8").val(true);
            }else{
                $("#ds8").attr("checked", false);
                $("#ds8").val(false);
            }
            if(json.ds9 == true){
                $("#ds9").attr("checked", true);
                $("#ds9").val(true);
            }else{
                $("#ds9").attr("checked", false);
                $("#ds9").val(false);
            }
            if(json.ds10 == true){
                $("#ds10").attr("checked", true);
                $("#ds10").val(true);
            }else{
                $("#ds10").attr("checked", false);
                $("#ds10").val(false);
            }
        }else{
            $("#divSystemInfo").html(data.message);
        }

    });
}

/**
 * 车号点击控制
 */
$("input[type='checkbox']").click(
    function() {
        $(this).attr("disable",true);
        var id = $(this).attr("id");
        var type = id.substring(0,2);
        var NO = id.substring(2,id.length);
        var carStatus = true;
        if($(this).is(":checked")){
            carStatus = true;
        }else{
            carStatus = false;
        }

        // alert(type + "|" + NO + "|" + carStatus);
        $.post("/setCarStaus",{"type":type,"NO":NO,"carStatus":carStatus},function(data){
            if( data.status == "success" ){

                $(this).val(carStatus);
                $(this).attr("disable",false);
                alert( id +"车修改成功,当前"+data.changeNow + "!");

            }else{
                alert( id +"车修改失败！");

            }

        });
    }
);


/**
 * 开车设置弹出
 */
$(".carSpan").find(".btn").click(
    function () {
        //获取车数据去最新穿版和基数
        var type = $(this).data("type");
        $("#setCarModalLabel").html(type + "车设置")
        var NO = type.substring(2,type.length);
        type = type.substring(0,2);
        $("#changeType").val(type);
        $("#changeNO").val(NO);
        $.post("/getNewIDDataOne",{"type":type,"NO":NO},function(data){
            if( data.status == "success" ){

                var json = data.data;
                $("#changeDieCount").val(json.dieCount);
                $("#changePreAmount").val(json.preAmount);
                $("#setCarModal").modal("show");
            }else{
                alert( type + NO +"车数据失败！");

            }

        });

    }
);


/**
 * 车数据修改保存
 */
$("#saveCarData").click(
    function () {

        if( $("#changeDieCount").val() <0 || $("#changePreAmount").val() <0 ){
            alert( "必须输入大于0的整数！");
            return;
        }
        $.post("/setCarData",{"type":$("#changeType").val(),"NO":$("#changeNO").val(),"dieCount": $("#changeDieCount").val() ,"preAmount": $("#changePreAmount").val()},function(data){
            if( data.status == "success" ){

                alert( $("#changeType").val() + $("#changeNO").val() +"车设置成功！");
                $("#setCarModal").modal("hide");
                getNewIDData();
                getDieCount();
            }else{
                alert( $("#changeType").val() + $("#changeNO").val() +"车设置失败！");

            }

        });

    }
);


/**
 * 清空穿版
 */
function clearDieCount(){

    $.post("/clearDieCount",function(data){

        if( data.status == "success" ){
            getDieCount();
            alert("清除穿版数量成功！");
        }else{
            alert("清除穿版数量失败！"+data.message);
        }

    });
}

/**
 * 设置返打
 * @constructor
 */
function SetReversal(){

    $.post("/setReversal",function(data){

        if( data.status == "success" ){
            if( data.data == true ){
                $("#setReversalSpan").html("反打：开");
                $("#btnSetReversal").removeClass("btn-success");
                $("#btnSetReversal").addClass("btn-warning");
            }else{
                $("#setReversalSpan").html("反打：关");
                $("#btnSetReversal").removeClass("btn-warning");
                $("#btnSetReversal").addClass("btn-success");
            }
            alert("设置反打成功！");
        }else{
            alert("设置反打失败！"+data.message);
        }

    });
}

/**
 * 设置真实下注
 * @constructor
 */
function SetRealBetFlag(){

    $.post("/setRealBetFlag",function(data){

        if( data.status == "success" ){
            if( data.data == "y" ){
                $("#realBetFlagSpan").html("真实下注：开");
                $("#btnRealBetFlag").removeClass("btn-success");
                $("#btnRealBetFlag").addClass("btn-warning");
                $("#realBetFlag").val(data.data);
            }else if( data.data == "n" ){
                $("#realBetFlagSpan").html("真实下注：关");
                $("#btnRealBetFlag").removeClass("btn-warning");
                $("#btnRealBetFlag").addClass("btn-success");
                $("#realBetFlag").val(data.data);
            }
            alert("设置真实下注成功！");
        }else{
            alert("设置真实下注失败！"+data.message);
        }

    });
}

/**
 * 查询真实下注状态
 * @constructor
 */
function GetRealBetFlag(){

    $.post("/getRealBetFlag",function(data){

        if( data.status == "success" ){
            if( data.data == "y" ){
                $("#realBetFlagSpan").html("真实下注：开");
                $("#btnRealBetFlag").removeClass("btn-success");
                $("#btnRealBetFlag").addClass("btn-warning");
                $("#realBetFlag").val(data.data);
            }else if( data.data == "n" ){
                $("#realBetFlagSpan").html("真实下注：关");
                $("#btnRealBetFlag").removeClass("btn-warning");
                $("#btnRealBetFlag").addClass("btn-success");
                $("#realBetFlag").val(data.data);
            }
        }else{

        }

    });
}

/**
 * 获取当前穿版次数
 */
function getDieCount(){

    $.post("/getDieCount",function(data){

        if( data.status == "success" ){
            $("#DieCountMessage").html("当前穿版数"+data.data + "次" );
            getNewIDData();
        }else{

        }

    });
}

/**
 * 获取当前穿版和基数
 */
function getNewIDData(){

    $.post("/getNewIDData",function(data){

        if( data.status == "success" ){
            $.each(data.data,function(i,json)
            {
                $("#status_"+json.type + json.NO).html( " 当前穿版" + json.dieCount + " | 当前基数" + json.preAmount + " ");
            });

        }else{
            // alert("获取当前穿版次数失败！"+data.message);
        }

    });
}


function setSelectDate() {
    $("#searchDate").val($("#searchDate").datepicker("getDate"));

}
